package com.oss.robot.link;

import java.util.ArrayList;
import java.util.List;

import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.LinkStringFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.HeadingTag;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;

import com.oss.model.Constants;
import com.oss.model.Link;
import com.oss.model.Task;
import com.oss.robot.AbstractRetrieveRobot;

/**
 * @author xueqiang.mi
 * @since 2010-8-1
 */
public class JavaWorldRobot extends AbstractRetrieveRobot {

	static public final String URL = "http://www.javaworld.com/";
	static public final String SOURCE = "JavaWorld.COM";
	static public final Integer TASK_NUMBER_PER_TIME = 2;

	@Override
	public void extractData(Task task) throws Exception {
		Parser parser = new Parser(task.getLink());
		NodeList nodeList = parser
				.extractAllNodesThatMatch(new HasAttributeFilter("id",
						"toplist"));
		nodeList = nodeList.elementAt(0).getChildren()
				.extractAllNodesThatMatch(new TagNameFilter("p"), true);

		for (Node node : nodeList.toNodeArray()) {
			NodeList list = node.getChildren().extractAllNodesThatMatch(
					new TagNameFilter("span"), true);
			if (list.size() != 2) {
				continue;
			}
			Link lnk = new Link();
			lnk.setSource(SOURCE);
			String desc = list.elementAt(0).getFirstChild().getText().trim();
			lnk.setDescription(desc);
			lnk.setAuthor(list.elementAt(1).getFirstChild().getText().trim());

			Node n = node.getChildren().elementAt(1);
			if (n instanceof LinkTag) {
				LinkTag linkTag = (LinkTag) n;
				lnk.setUrl("http://www.javaworld.com" + linkTag.getLink());
				lnk.setName(linkTag.getLinkText());
			} else {
				n = node.getPreviousSibling();
				while (!(n instanceof HeadingTag)) {
					n = n.getPreviousSibling();
				}
				LinkTag linkTag = (LinkTag) n.getChildren().elementAt(1);
				lnk.setUrl("http://www.javaworld.com" + linkTag.getLink());
				lnk.setName(linkTag.getLinkText());
			}
			lnkService.add(lnk);
		}
	}

	@Override
	public String getSource() {
		return SOURCE;
	}

	@Override
	public Integer getTasksNumber() {
		return TASK_NUMBER_PER_TIME;
	}

	@Override
	public List<Task> retrieveLinks() throws Exception {
		Parser parser = new Parser(URL);
		NodeList nodeList = parser.extractAllNodesThatMatch(new AndFilter(
				new LinkStringFilter("/"), new HasAttributeFilter("shape",
						"rect")));
		List<Task> tasks = new ArrayList<Task>();
		for (int i = 1; i < nodeList.size(); i++) {
			LinkTag link = (LinkTag) nodeList.elementAt(i);
			Task task = new Task(link.getLink(), SOURCE, Constants.ENTRY_LINK);
			tasks.add(task);
		}
		return tasks;
	}
}
